import { defineStore } from 'pinia'
import { ref } from 'vue'
import { reqC1, reqC2, reqC3 } from '@/api/product/attr'
import type { CategoryResponseData, CategoryObj } from '@/api/product/type'

let useCategoryStore = defineStore('Category', () => {
  type IdType = string | number
  const c1Arr = ref<CategoryObj[]>([]) // 一级分类的数据
  const c1Id = ref<IdType>('') // 一级分类的ID
  const c2Arr = ref<CategoryObj[]>([]) // 二级分类的数据
  const c2Id = ref<IdType>('') // 二级分类的ID
  const c3Arr = ref<CategoryObj[]>([]) // 三级分类的数据
  const c3Id = ref<IdType>('') // 三级分类的ID

  const getC1 = async () => {
    const result: CategoryResponseData = await reqC1()
    if (result.code == 200) {
      c1Arr.value = result.data
    }
  }
  const getC2 = async () => {
    const result: CategoryResponseData = await reqC2(c1Id.value)
    if (result.code == 200) {
      c2Arr.value = result.data
    }
  }
  const getC3 = async() => {
    const result: CategoryResponseData = await reqC3(c2Id.value)
    if (result.code == 200) {
      c3Arr.value = result.data
    }
  }
  return {
    c1Arr, c1Id, getC1,
    c2Arr, c2Id, getC2,
    c3Arr, c3Id, getC3,
  }
})

export default useCategoryStore
